var hosturl="http://www.ihggccampaign.com/2018spring/";

var sharerandom = Math.floor(Math.random()*2);
var sharetxt=[
	{title:"春天在哪里？" , desc:"羡慕春风十里不如立马春游走起～春日出游指南看这里！" , source:"opt1"},
	{title:"遇见心动春日？" , desc:"春天来了，不如出去浪吧～精选春游目的地任你挑选！" , source:"opt2"}
]

$(function (){
	$("title").html(sharetxt[sharerandom].title)
	})

var shareData = {
			title: sharetxt[sharerandom].title,
			desc: sharetxt[sharerandom].desc,
			link: hosturl+"index.html?share=share&source="+sharetxt[sharerandom].source,
			imgUrl: hosturl+"images/share.jpg"
		};


		
var wxurl = location.href.split('#')[0];

wxconfig();
function wxconfig(config, apilist) {
	
    if (!apilist) apilist = ['onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo' ];
    if (config && config.appId && config.timestamp && config.nonceStr && config.signature) {
        wx.config({
            debug: false,
            appId: config.appId,
            timestamp: config.timestamp,
            nonceStr: config.nonceStr,
            signature: config.signature,
            jsApiList: apilist
        });
    }
    else {
        $.ajax({
            type: "GET",
            url: "http://www.ihggccampaign.com/2017winter/wx_api/wx.php?id=signPackage",
            async: true,
            dataType: "json",
			data: {url:wxurl},
            success: function (data) {
				var data=eval(data);
				//alert( JSON.stringify(data)+" *** 测试2")
                if (data) {
                    //alert(data.appId +"**"+ data.timestamp +"**"+ data.nonceStr +"**"+ data.signature);
                    if (data.appId && data.timestamp && data.nonceStr && data.signature) {
                        wx.config({
                            debug: false,
                            appId: data.appId,
                            timestamp: data.timestamp,
                            nonceStr: data.nonceStr,
                            signature: data.signature,
                            jsApiList: apilist
                        });
						
						MyIp=data.ip;
						
						//alert(MyIp);
                    }
                    else {
                     // alert(1)  
                    }
                }
				//"http://www.ihggccampaign.com/wx_api/wx.php?type=signPackage",
				//"http://www.ihggccampaign.com/spring2016/wx_api/vendor.php?id=signPackage"
            },
            error: function (xhr, msg, exc) {
				
                //alert("生成微信签名失败，请重试！");
            }
        });
    }
}

function wxevent(data) {
    if (!data) data = shareData;
    
	//shareFun(shareData);// 分享
	wx.onMenuShareAppMessage({
        title: data.title,
        desc: data.desc,
        link: data.link,
        imgUrl: data.imgUrl,
        trigger: function (res) {
            //alert('用户点击发送给朋友');
			ga('send', 'event', 'button', 'click', "share_success");
        },
        success: function (res) {
            //alert('已分享');
			
			shareSuccess()
        },
        cancel: function (res) {
            //alert('已取消');
        },
        fail: function (res) {
            //alert(JSON.stringify(res));
        }
    });
    // 2.2 监听“分享到朋友圈”按钮点击、自定义分享内容及分享结果接口
    wx.onMenuShareTimeline({
        title: data.desc,
		desc: " ",
        link: data.link,
        imgUrl: data.imgUrl,
        trigger: function (res) {
			
			ga('send', 'event', 'button', 'click', "share_success");
            //alert('用户点击分享到朋友圈');
			//_hmt.push(['_trackEvent', 'button', 'click', 'onMenuShareTimeline']);	
        },
        success: function (res) {
            //alert('已分享');
			
			shareSuccess()
        },
        cancel: function (res) {
            //alert('已取消');
        },
        fail: function (res) {
            //alert(JSON.stringify(res));
        }
    });
    // 2.3 监听“分享到QQ”按钮点击、自定义分享内容及分享结果接口
    wx.onMenuShareQQ({
        title: data.title,
        desc: data.desc,
        link: data.link,
        imgUrl: data.imgUrl,
        trigger: function (res) {
            //alert('用户点击分享到QQ');
        },
        complete: function (res) {
            //alert(JSON.stringify(res));
        },
        success: function (res) {
            //alert('已分享');
        },
        cancel: function (res) {
            //alert('已取消');
        },
        fail: function (res) {
            //alert(JSON.stringify(res));
        }
    });

    // 2.4 监听“分享到微博”按钮点击、自定义分享内容及分享结果接口
    wx.onMenuShareWeibo({
        title: data.title,
        desc: data.desc,
        link: data.link,
        imgUrl: data.imgUrl,
        trigger: function (res) {
            //alert('用户点击分享到微博');
        },
        complete: function (res) {
            //alert(JSON.stringify(res));
        },
        success: function (res) {
            //alert('已分享');
        },
        cancel: function (res) {
            //alert('已取消');
        },
        fail: function (res) {
            //alert(JSON.stringify(res));
        }
    });
	
	


}
wx.ready(function () {

	 wxevent(); 

	 });
wx.error(function (res) {
    //alert(JSON.stringify(res));
    // config信息验证失败会执行error函数，如签名过期导致验证失败，具体错误信息可以打开config的debug模式查看，也可以在返回的res参数中查看，对于SPA可以在这里更新签名。
});

	
